/*
 * This file is part of aion-unique <aion-unique.org>.
 *
 *  aion-unique is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  aion-unique is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with aion-unique.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.aionemu.gameserver.ai.npcai;

import com.aionemu.gameserver.ai.AI;
import com.aionemu.gameserver.ai.desires.impl.AttackDesire;
import com.aionemu.gameserver.ai.desires.impl.MoveToTargetDesire;
import com.aionemu.gameserver.ai.events.Event;
import com.aionemu.gameserver.ai.events.handler.EventHandler;
import com.aionemu.gameserver.ai.state.AIState;
import com.aionemu.gameserver.ai.state.handler.NoneNpcStateHandler;
import com.aionemu.gameserver.ai.state.handler.StateHandler;
import com.aionemu.gameserver.model.gameobjects.Creature;
import com.aionemu.gameserver.model.gameobjects.Homing;
import com.aionemu.gameserver.model.gameobjects.Npc;

/**
 * @author ATracer
 * 
 */
public class HomingAi extends NpcAi
{
	public HomingAi()
	{
		/**
		 * Event handlers
		 */
		this.addEventHandler(new RespawnEventHandler());

		/**
		 * State handlers
		 */
		this.addStateHandler(new ActiveHomingStateHandler());
		this.addStateHandler(new NoneNpcStateHandler());
	}

	public class RespawnEventHandler implements EventHandler
	{
		@Override
		public Event getEvent()
		{
			return Event.RESPAWNED;
		}

		@Override
		public void handleEvent(Event event, AI<?> ai)
		{
			ai.setAiState(AIState.ACTIVE);

			if(!ai.isScheduled())
				ai.analyzeState();
		}

	}

	class ActiveHomingStateHandler extends StateHandler
	{
		@Override
		public AIState getState()
		{
			return AIState.ACTIVE;
		}

		@Override
		public void handleState(AIState state, AI<?> ai)
		{
			//ai logic is rather strange but ok till global refactoring
			ai.clearDesires();
			Homing homing = (Homing) ai.getOwner();
			Creature target = (Creature) homing.getTarget();
			if(target == null || target.getLifeStats().isAlreadyDead()){
				return;
			}
			ai.getOwner().getAggroList().addHate(target, 1);
			target.getAggroList().addHate(homing.getActingCreature(), 1);
			ai.addDesire(new HomingAttackDesire(homing, target, AIState.ATTACKING.getPriority()));
			ai.addDesire(new MoveToTargetDesire(homing, target, AIState.ATTACKING.getPriority()));

			if(!ai.isScheduled())
				ai.schedule();
		}
	}

	private final class HomingAttackDesire extends AttackDesire
	{
		public HomingAttackDesire(Npc npc, Creature target, int desirePower)
		{
			super(npc, target, desirePower);
		}

		@Override
		public int getExecutionInterval()
		{
			return 4;
		}
	}

}
